###############################################################################
#
# Main Makefile for GIMnetAP.
# (c) Antti Maula <antti.maula@tkk.fi>
#
###############################################################################

CC=gcc
CFLAGS=-O2 -g3 -Wall -Iprotocol -I../utils -I$(GIMNETAP_COMMON_LIBDIR) -DLINUX_OS	\
-DUSE_SELECT_IO $(CE)
LD=gcc
LDFLAGS=
LIBS=-lpthread -lrt -lssl -lcrypto

# Sources
SRCSBASE=GIMnetAP.c connectionDisp.c connectionList.c						\
	connectionListEntry.c protocol.c routing.c instrumentation.c	\
	delayedlink.c ../utils/ownutils.c ../utils/owndebug.c					\
	servicelauncher.c ../utils/crc16.c

SRCSPROTO=protocol/fastProtocol.c protocol/fastProtocol_util.c	\
	protocol/fastProtocolExplicitMulticast.c											\
	protocol/fastProtocolBroadcast.c protocol/hubctrl.c						\
	protocol/hubctrl_process.c protocol/protocolID.c							\
	protocol/hubctrl_utils.c

GIMNETAP_COMMON_LIBDIR=./commonlib
include ./commonlib/GIMnetAP-common.Makefile


SRCS=$(SRCSBASE) $(SRCSPROTO) $(GIMNETAP_COMMON_SRCS)

# Construct lists of CPP and C objects
OBJS=$(SRCS:.c=.o)

# Define COMPILE and LINK commands
COMPILE=$(CC) $(CFLAGS) -c
LINK=$(LD) $(LDFLAGS) 

all: GIMnetAP testclients services protocol

.PHONY: include
include:
	mkdir -p ./include
	rm -fv ./include/*.h
	cp -v $(GIMNETAP_COMMON_LIBDIR)/*.h ./include
	cp -v protocol/*Hdr*.h protocol/protocolID.h ./include
	cp -v services/*/ap*frame.h ./include
	cp -v services/nameservice/nametableentry.h ./include

	mkdir -p ../include/GIMnetAP
	rm -fv ../include/GIMnetAP/*.h
	cp -v ./include/* ../include/GIMnetAP
	chmod -v 440 ../include/GIMnetAP/*.h

GIMnetAP: $(OBJS)
	@$(LINK) -o $@ $(OBJS) $(LIBS)
	@echo "      [LD] $@"

.PHONY: services services_clean protocol protocol_clean
services:
	@+cd services; make
services_clean:
	@+cd services; make clean
protocol:
	@+cd protocol; make
protocol_clean:
	@+cd protocol; make clean

clean: services_clean protocol_clean
	@rm -f $(OBJS) *~ protocol/*~ GIMnetAP
	@echo "      [CLEAN] GIMnetAP"

%.o:	%.c %.h
	@$(COMPILE) -o $@ $<
	@echo "      [CC] $<"

.PHONY: all clean depend testclients

##############################################
### About working Dependency generation sequence
##############################################
LOCAL_SOURCES_C=`find . -maxdepth 1 -iname '*.c'`
.PHONY: depend
depend:
	touch Makefile.c.dep
	makedepend -f Makefile.c.dep -o.o -- $(CFLAGS) -- *.c $(SRCSC) $(LOCAL_SOURCES_C) >/dev/null 2>&1
###############################################
-include Makefile.c.dep
##############################################
